package com.zhxy.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.dao.SongDao;
import com.zhxy.manager.entity.Library;
import com.zhxy.manager.entity.Song;
import com.zhxy.manager.service.intf.LibraryService;
import com.zhxy.manager.service.intf.SongService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/song/")
public class SongController {
    @Autowired
    private SongService service;

    //新增
    @RequestMapping("save.do")
    public R save(Song song){
        return service.save(song);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(int page, int limit){

        return service.queryPage(page, limit);
    }
    //条件查询
    @GetMapping("pagewhere.do")
    public PageBean page(String name,int page,int limit){
        return service.queryPageWhere(name,page,limit);
    }
    //删除
    @PostMapping("del.do")
    public R del(int id){
        return  service.del(id);
    }
    //修改
    @PostMapping("update.do")
    public R update(Song song){
        return service.update(song);
    }
    //批量删除 JSON格式的数据请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids){
        return service.delBatch(ids);
    }


    //数据到处文件下载
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Song> list=service.selectAll();
        //2.设置响应消息头  时间戳作为文件名  防止重复
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容p
        //下载，写道输出流里面 编译时异常爬出去
        EasyExcel.write(response.getOutputStream(),Song.class).
                sheet("自动导出").doWrite(list);
    }
    //数据导入，E 全局异常处理器文件上传必须要用@PostMapping
    @PostMapping("upload.do")
    //把MultipartFire换成数组[] 就能上传多个 最好单个
    public R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
    }


}
